﻿#region

using System.Web.Mvc;
using CuongHanh.Models;
using CuongHanh.Security;
using CuongHanh.Service;
using Ext.Net.MVC;

#endregion

namespace CuongHanh.Controllers.BO
{
    [CustomAuthorize(Key = "Management", Description = "Manage all apps")]
    public class AMyStoneController : ABaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ShowAdd(string containerId)
        {
            return ShowWindow("Add",
                    containerId,
                    new MyStone());
        }

        [HttpPost]
        public ActionResult Add(MyStone model)
        {
            MyStone newModel = new MyStone(model);
            if (Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                newModel.Image = fileName;
            }
            UpdateResult result = MyStoneService.Insert(newModel);
            ShowWindow("wdModify",
                    false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult Command(string containerId,
                string command,
                int id)
        {
            if(command == "Delete")
            {
                UpdateResult result = MyStoneService.Delete(id);
                ReloadStore("storeGvData");
                ShowNotification(result);
            }
            if(command == "Edit")
            {
                MyStone model = MyStoneService.Get(id);
                return ShowWindow("Edit",
                        containerId,
                        model);
            }
            return this.Direct();
        }

        [HttpPost]
        public ActionResult Edit(MyStone model)
        {
            if (Request.Files.Count > 0
               && Request.Files[0].ContentLength > 0)
            {
                byte[] imgData = new byte[Request.Files[0].ContentLength];
                Request.Files[0].InputStream.Read(imgData,
                        0,
                        Request.Files[0].ContentLength);
                string fileName = WriteByteToFileForGem(imgData,
                        Request.Files[0].InputStream);
                model.Image = fileName;
            }
            UpdateResult result = MyStoneService.Update(model);
            ShowWindow("wdModify",
                    false);
            ReloadStore("storeGvData");
            ShowNotification(result);
            return this.Direct();
        }

        public ActionResult GetDatas(int start,
                int limit,
                string query)
        {
            return Content(MyStoneService.Gets(start,
                    limit,
                    query),
                    "json");
        }
    }
}
